package cn.com.zcode.exam.control;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import cn.com.zcode.core.control.BaseActionSupport;
import cn.com.zcode.dic.control.ApplicationCache;
import cn.com.zcode.dic.model.Dictionary;
import cn.com.zcode.exam.model.Answer;
import cn.com.zcode.exam.model.Examination;
import cn.com.zcode.exam.model.Paper;
import cn.com.zcode.exam.model.Question;
import cn.com.zcode.exam.model.StaticInfo;
import cn.com.zcode.exam.model.Wrong;
import cn.com.zcode.exam.service.ExamService;
import cn.com.zcode.user.model.User;

public class ExamAction extends BaseActionSupport<ExamService> {
	private static final long serialVersionUID = 1L;
	private Examination examination;
	private Question question;
	private Answer answer;
	private boolean sealExam;
	private boolean continueExam;

	public ExamAction() {
		super();
	}

	public String myWrongs() {
		String type = "pmp";
		User user = this.getSessionAttribute("user", User.class);
		List<Question> list = this.service.findWrongQuestions(type, user.getId());
		List<Dictionary> dlist = ApplicationCache.getDictionaryByType(type);

		Map<String, StaticInfo> map = new LinkedHashMap<String, StaticInfo>();
		for (Dictionary dictionary : dlist) {
			map.put(dictionary.getCode(), new StaticInfo(dictionary.getTitle(), 0));
		}
		for (Question quest : list) {
			StaticInfo info = map.get(quest.getChapter());
			if (info != null) {
				info.setNum(info.getNum() + 1);
			}
		}

		StringBuffer subxml = new StringBuffer(
				"<chart  maxColWidth ='70' placeValuesInside ='1' caption='错题分布' labelStep='1' xAxisName='章节' yAxisName='数量'  showValues='1' numberSuffix ='题' baseFontSize ='12' rotateNames ='true'   >");
		Set<String> set = map.keySet();
		for (String key : set) {
			StaticInfo staticInfo = map.get(key);
			subxml.append("<set label='" + staticInfo.getTitle() + "' value='" + staticInfo.getNum() + "' />");
		}

		subxml.append("</chart> ");
		this.addAttribute("staXmls", subxml);
		addAttribute("questions", list);

		return SUCCESS;
	}

	public String startExam() {
		User user = this.getSessionAttribute("user", User.class);
		String userid = user.getId();
		if (examination == null) {
			List<Examination> papers = service.findExamination("pmp",user.getId());
			this.addAttribute("papers", papers);
			return INPUT;
		} else {
			String eid = examination.getId();
			String pid = examination.getPaperId();
			examination.setUserId(userid);
			List<Question> questions =null;
			if (Paper.PAPER_WRONG_INFO.equals(pid)) {
				Paper paper = new Paper();
				paper.setUserId(userid);
				paper.setState(2);
				paper.setTitle("我的错题");
				paper.setType(examination.getType());
				questions = service.findWrongQuestions(examination.getType(), userid);
				paper.addQuestions(questions);
				this.service.mergePaper(paper);
				examination.setStaTime(new Date());
			}else{
				if (eid == null || (eid = eid.trim()).isEmpty()||!continueExam) {
					if(eid!= null&&!eid.isEmpty()&&!continueExam){
						service.saveFinishExamination(eid);
						examination.setId(null);
					}
					if(examination.getState() == Paper.STATE_WRONG){
						questions = service.findWrongQuestions(examination.getType(), user.getId());
						Paper paper = service.getPaper(pid);
						paper.getQuestions().clear();
						paper.addQuestions(questions);
						this.service.mergePaper(paper);
						examination.setPaperId(paper.getId());
					}else{
						questions = service.findNewExamQuestions(pid);
					}
					examination.setStaTime(new Date());
					service.addExamination(examination);
				} else {
					questions = service.findLastExamQuestions(eid);
				}
				
				
			}
			this.addAttribute("isWrongInfos", examination.getState() == Paper.STATE_WRONG);
			examination.addQuestions(questions);
		}
		return SUCCESS;
	}

	public String submitAnswer() {
		answer.setAnswerDate(new Date());
		answer = service.addAnswer(answer);
		int anstate = answer.getState();
		boolean iscorrect = answer.isCorrect();
		if (anstate == Answer.STATE_UNSURE || !iscorrect||anstate == Answer.STATE_CANFIXED) {
			String questionId = answer.getQuestionId();
			String userId = answer.getUserId();
			//String paperId = this.examination.getPaperId();
			Wrong wrong = service.getWrong(userId, questionId);
			if (wrong == null) {
				wrong = new Wrong(answer);
				// this.service.addPQConnector(new PQConnector(questionId, paperId));
			}
			if (answer.isCorrect() && anstate == Answer.STATE_CANFIXED) {
				wrong.setFixed(true);
				// this.service.delPQConnector(questionId,paperId);
			} else if (anstate ==  Answer.STATE_UNSURE) {
				wrong.setUnsure(true);
			} else {
				wrong.addCount();
			}
			service.addWrong(wrong);
		}
		return SUCCESS;
	}

	public String addQuestion() {
		this.service.addQuestion(question);
		return SUCCESS;
	}

	public Question getQuestion() {
		return question;
	}

	public void setQuestion(Question question) {
		this.question = question;
	}

	public Answer getAnswer() {
		return answer;
	}

	public void setAnswer(Answer answer) {
		this.answer = answer;
	}

	public Examination getExamination() {
		return examination;
	}

	public void setExamination(Examination examination) {
		this.examination = examination;
	}

	public boolean isSealExam() {
		return sealExam;
	}

	public void setSealExam(boolean sealExam) {
		this.sealExam = sealExam;
	}

	public boolean isContinueExam() {
		return continueExam;
	}

	public void setContinueExam(boolean continueExam) {
		this.continueExam = continueExam;
	}

}
